#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "pwm.h"
#include "key.h"


//定义音调名，方便写谱
#define V0  0
#define L1  1
#define L1_ 2
#define L2  3
#define L2_ 4
#define L3  5
#define L4  6
#define L4_ 7
#define L5  8
#define L5_ 9
#define L6  10
#define L6_ 11
#define L7  12

#define M1  13
#define M1_ 14
#define M2  15
#define M2_ 16
#define M3  17
#define M4  18
#define M4_ 19
#define M5  20
#define M5_ 21
#define M6  22
#define M6_ 23
#define M7  24

#define H1  25
#define H1_ 26
#define H2  27
#define H2_ 28
#define H3  29
#define H4  30
#define H4_ 31
#define H5  32
#define H5_ 33
#define H6  34
#define H6_ 35
#define H7  36

//定义音调表
uint16_t tone[]={
	0,
	3817,3610,3401,3215,3030,2865,2703,2551,2410,2272,2146,2024,
	1912,1805,1703,1608,1517,1432,1351,1275,1203,1136,1073,1012,
	956,902,851,803,758,715,676,637,602,568,536,506,100
};

//定义简谱表：音调,时值
uint16_t Music[]={
//孤独摇滚 吉他与孤独与蓝色星球（70）
	H1,2,M7,2,M6,2,M7,4,M1,2,M2,2,M3,4,M1,4,L7,4,L7,2,L7,4,
	H1,2,M7,2,M6,2,M7,4,M2,2,M3,4,M1,2,M5,4,M6,2,M5,4,
	H1,2,M7,2,M6,2,M7,4,M1,2,M2,2,M3,4,M1,4,L7,4,L7,2,L7,4,
	H2,4,H3,4,M6,4,H1,2,H2,4,H3,4,H5,4,H3,4,
	H6,4,H5,4,H2,2,H3,4,H3,4,M6,4,M5,2,M6,4,M6,4,
	
	M6,4,M6,2,M6,2,M6,2,M5,2,M5,2,M5,2,M3,4,M5,4,M3,4,M5,4,M3,6,
	M5,2,M3,6,M5,2,M3,6,M5,2,M3,6,M2,2,M1,10,
	M1,4,H1,4,M7,4,M5,4,M3,4,M2,4,M3,4,M2,4,M1,4,M3,6,M3,2,M2,8,
	
	M6,4,M6,2,M6,2,M6,2,M5,2,M5,2,M5,2,M3,4,M5,4,M3,4,M5,4,M3,6,
	M5,2,M3,6,M5,2,M3,6,M5,2,M3,6,M2,2,M1,10,
	M1,4,H1,4,M7,4,M5,4,M3,4,M2,4,M3,4,M2,4,M1,4,M3,6,M4,2,M2,8,
	
	M2,6,M1,2,M1,6,M1,2,M2,6,M1,2,M1,6,M1,2,M2,4,M1,4,M2,4,M5,4,M2,6,M1,2,M1,4,M1,6,
	M1,2,M2,6,M1,2,M1,4,M1,4,M2,6,M1,2,M1,4,M1,4,M2,4,M1,4,M2,4,M5,4,M6,4,M5,2,M5,2,M5,2,	
	M5,2,M6,6,M5,2,M5,4,M5,4,M6,6,M5,2,M5,4,M5,4,M6,4,M5,4,M6,4,H1,4,M7,4,M5,4,M3,4,
	M5,4,M1,12,M3,4,M2,2,M1,2,M1,6,M2,4,M3,6,M2,2,M2,8,M4,6,M3,2,M3,12,
	
	H1,4,M7,4,M5,4,M5,2,M6,2,M6,4,M5,2,M6,2,M6,4,M7,4,H1,4,H2,4,M7,4,M7,4,H1,4,M7,4,M5,4,M3,4,	
	M3,4,M6,2,M5,2,M3,2,M1,2,M2,2,M3,4,M1,2,M2,2,M3,2,M1,4,M2,2,M3,2,M1,4,M2,2,M3,2,M5,2,M3,2,M2,4,M3,4,M2,4,M1,4,M3,8,
	M6,4,M5,4,M5,2,M6,2,M6,4,M5,2,M6,2,M6,4,M7,4,H1,4,H2,4,M7,4,M5,4,H4,6,H3,2,H3,4,
	H1,4,H2,4,H3,4,H4,2,H3,2,H3,2,H2,2,H1,2,M7,2,M7,4,M6,2,M7,2,M7,4,H1,2,H1,4,H1,16,V0,8,
	H1,4,H2,4,H3,4,H4,2,H3,2,H3,2,H2,2,H1,2,M7,8,V0,2,M5,2,M7,4,H1,4,

	//结束标志位
	V0,16,0xFF  
};	


uint16_t choose,time;  //用于遍历数组
uint16_t keynum;  //用于按键判断

void show_tone(void);

int main(void)
{
	OLED_Init();
	key_init();
	OLED_ShowString(1,1,"Bocchi the Rock!");	
	//按键开启蜂鸣器，按键B0,蜂鸣器A0
	while(1)
	{
		keynum=get_keynum();
		if(keynum ==1)
		{
			OLED_Clear();
			OLED_ShowString(1,1,"Time:");
			OLED_ShowString(2,1,"Tone:");
			pwm_init();		
			break;
		}
	}
	
	//遍历数组播放音乐
	while(2)
	{
		//下方time++两次，好比是二维数组		
		//第一次取的是tone，第二次取的是time，即：让tone延时time微秒
		choose=Music[time];  
		time++;  //1
		pwm_settone(tone[choose]);					
		Delay_ms(77.5*Music[time]);    //数字修改1/16时值
		time++;  //2				
		OLED_ShowNum(1,6,time,4);
		show_tone();
	}
}

//显示音调
void show_tone(void)
{
	switch (Music[time])
    {
    	case 0:OLED_ShowString(2,6,"---");
    		break;		
    	case 1:OLED_ShowString(2,6,"-C ");
    		break;
    	case 2:OLED_ShowString(2,6,"-C#");
    		break;
    	case 3:OLED_ShowString(2,6,"-D ");
    		break;
    	case 4:OLED_ShowString(2,6,"-D#");
    		break;
    	case 5:OLED_ShowString(2,6,"-E ");
    		break;
    	case 6:OLED_ShowString(2,6,"-F ");
    		break;
    	case 7:OLED_ShowString(2,6,"-F#");
    		break;
    	case 8:OLED_ShowString(2,6,"-G ");
    		break;
    	case 9:OLED_ShowString(2,6,"-G#");
    		break;
    	case 10:OLED_ShowString(2,6,"-A ");
    		break;
    	case 11:OLED_ShowString(2,6,"-A#");
    		break;
    	case 12:OLED_ShowString(2,6,"-B ");
    		break;
    	case 13:OLED_ShowString(2,6," C ");
    		break;
    	case 14:OLED_ShowString(2,6," C#");
    		break;
    	case 15:OLED_ShowString(2,6," D ");
    		break;
    	case 16:OLED_ShowString(2,6," D#");
    		break;
    	case 17:OLED_ShowString(2,6," E ");
    		break;
    	case 18:OLED_ShowString(2,6," F ");
    		break;
    	case 19:OLED_ShowString(2,6," F#");
    		break;
    	case 20:OLED_ShowString(2,6," G ");
    		break;
    	case 21:OLED_ShowString(2,6," G#");
    		break;
    	case 22:OLED_ShowString(2,6," A ");
    		break;
    	case 23:OLED_ShowString(2,6," A#");
    		break;
    	case 24:OLED_ShowString(2,6," B ");
    		break;
    	case 25:OLED_ShowString(2,6,"+C ");
    		break;
    	case 26:OLED_ShowString(2,6,"+C#");
    		break;
    	case 27:OLED_ShowString(2,6,"+D ");
    		break;
    	case 28:OLED_ShowString(2,6,"+D#");
    		break;
    	case 29:OLED_ShowString(2,6,"+E ");
    		break;
    	case 30:OLED_ShowString(2,6,"+F ");
    		break;
    	case 31:OLED_ShowString(2,6,"+F#");
    		break;
    	case 32:OLED_ShowString(2,6,"+G ");
    		break;
    	case 33:OLED_ShowString(2,6,"+G#");
    		break;
    	case 34:OLED_ShowString(2,6,"+A ");
    		break;
    	case 35:OLED_ShowString(2,6,"+A#");
    		break;
    	case 36:OLED_ShowString(2,6,"+B ");
    		break;
		default:
    		break;
    }
}	
